package com.openbravo.pos.printer.escpos;

import java.awt.image.BufferedImage;

import com.openbravo.pos.printer.DevicePrinter;

public abstract class AbstractDevicePrinterESCPOS implements DevicePrinter{

	protected PrinterWritter m_CommOutputPrinter;
	protected Codes m_codes;
	protected UnicodeTranslator m_trans;
	protected String m_sName;


	public String getPrinterName() {
	    return m_sName;
	}

	public String getPrinterDescription() {
	    return null;
	}

	public void reset() {
	}

	public void beginReceipt() {
	}

	public void printImage(BufferedImage image) {
	    
	    m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);        
	    m_CommOutputPrinter.write(m_codes.transImage(image));
	}

	public void printBarCode(String type, String position, String code) {
	    
	    m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);        
	    m_codes.printBarcode(m_CommOutputPrinter, type, position, code);
	}

	public void beginLine(int iTextSize) {
	
	    m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);        
	    
	    if (iTextSize == DevicePrinter.SIZE_0) {
	        m_CommOutputPrinter.write(m_codes.getSize0());
	    } else if (iTextSize == DevicePrinter.SIZE_1) {
	        m_CommOutputPrinter.write(m_codes.getSize1());
	    } else if (iTextSize == DevicePrinter.SIZE_2) {
	        m_CommOutputPrinter.write(m_codes.getSize2());
	    } else if (iTextSize == DevicePrinter.SIZE_3) {
	        m_CommOutputPrinter.write(m_codes.getSize3());
	    } else {
	        m_CommOutputPrinter.write(m_codes.getSize0());
	    }
	}

	public void printText(int iStyle, String sText) {
	
	    m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);   
	
	    if ((iStyle & DevicePrinter.STYLE_BOLD) != 0) {
	        m_CommOutputPrinter.write(m_codes.getBoldSet());
	    }
	    if ((iStyle & DevicePrinter.STYLE_UNDERLINE) != 0) {
	        m_CommOutputPrinter.write(m_codes.getUnderlineSet());
	    }
	    m_CommOutputPrinter.write(m_trans.transString(sText));
	    if ((iStyle & DevicePrinter.STYLE_UNDERLINE) != 0) {
	        m_CommOutputPrinter.write(m_codes.getUnderlineReset());
	    }
	    if ((iStyle & DevicePrinter.STYLE_BOLD) != 0) {
	        m_CommOutputPrinter.write(m_codes.getBoldReset());
	    }     
	}

	public void endLine() {
	    m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);   
	    m_CommOutputPrinter.write(m_codes.getNewLine());
	}

	public void endReceipt() {
	    m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);   
	    
	    //m_CommOutputPrinter.write(m_codes.getNewLine());
	    //m_CommOutputPrinter.write(m_codes.getNewLine());
	    //m_CommOutputPrinter.write(m_codes.getNewLine());
	    //m_CommOutputPrinter.write(m_codes.getNewLine());
	    //m_CommOutputPrinter.write(m_codes.getNewLine());
	
	    //m_CommOutputPrinter.write(m_codes.getCutReceipt());
	    m_CommOutputPrinter.flush();
	}

	public void openDrawer() {
	
	    m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);   
	    m_CommOutputPrinter.write(m_codes.getOpenDrawer());
	    m_CommOutputPrinter.flush();
	}

	@Override
	public void cutReceipt() {
		 m_CommOutputPrinter.write(ESCPOS.SELECT_PRINTER);   
		 m_CommOutputPrinter.write(m_codes.getCutReceipt());
	}
}